package com.anjie.powerproject.map.impl;

import com.anjie.powerproject.entity.DynamicProperty;
import com.anjie.powerproject.map.DynamicPropertyMap;
import com.anjie.powerproject.pojo.dto.DynamicPropertyDTO;
import lombok.val;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;

@Component
public class DynamicPropertyMapImpl implements DynamicPropertyMap {

    @Override
    public DynamicPropertyDTO toDTO(DynamicProperty dynamicProperty) {
        if ( dynamicProperty == null ) {
            return null;
        }

        DynamicPropertyDTO dynamicPropertyDTO = new DynamicPropertyDTO();

        dynamicPropertyDTO.setId( dynamicProperty.getId() );
        dynamicPropertyDTO.setIsOpen( dynamicProperty.getIsOpen() );
        dynamicPropertyDTO.setGraphId( dynamicProperty.getGraphId() );
        dynamicPropertyDTO.setTargetResId( dynamicProperty.getTargetResId() );
        dynamicPropertyDTO.setWorkTime( dynamicProperty.getWorkTime() );

        //把String转化成List<String>
        if(dynamicProperty.getDate()!=null&&dynamicProperty.getDate()!="") {
            String[] SelectDate = dynamicProperty.getDate().split(",");
            List<String> list = new ArrayList<>();
            for (String s : SelectDate) {
                list.add(s);
            }
        dynamicPropertyDTO.setDate(list);
        }

        return dynamicPropertyDTO;
    }

    @Override
    public List<DynamicPropertyDTO> toDTO(List<DynamicProperty> dynamicProperties) {
        if ( dynamicProperties == null ) {
            return null;
        }

        List<DynamicPropertyDTO> list = new ArrayList<DynamicPropertyDTO>( dynamicProperties.size() );
        for ( DynamicProperty dynamicProperty : dynamicProperties ) {
            list.add( toDTO( dynamicProperty ) );
        }

        return list;
    }

    @Override
    public DynamicProperty toEntity(DynamicPropertyDTO propertyDTO) {
        if ( propertyDTO == null ) {
            return null;
        }

        DynamicProperty dynamicProperty = new DynamicProperty();

        dynamicProperty.setExpress( toExpress( propertyDTO.getDetails() ) );
        dynamicProperty.setId( propertyDTO.getId() );
        dynamicProperty.setGraphId( propertyDTO.getGraphId() );
        dynamicProperty.setIsOpen( propertyDTO.getIsOpen() );
        dynamicProperty.setTargetResId( propertyDTO.getTargetResId() );
        dynamicProperty.setWorkTime( propertyDTO.getWorkTime() );

        //将List<String>转化成String
        if(propertyDTO.getDate().size()!=0){
            StringBuilder result = new StringBuilder();

            boolean flag = false;
            for (String str : propertyDTO.getDate()) {
                if (flag) result.append(",");
                else flag = true;

                result.append(str);
            }
            dynamicProperty.setDate(result.toString());
        }

        return dynamicProperty;
    }

    @Override
    public List<DynamicProperty> toEntity(List<DynamicPropertyDTO> propertyDTOS) {
        if ( propertyDTOS == null ) {
            return null;
        }

        List<DynamicProperty> list = new ArrayList<DynamicProperty>( propertyDTOS.size() );
        for ( DynamicPropertyDTO dynamicPropertyDTO : propertyDTOS ) {
            list.add( toEntity( dynamicPropertyDTO ) );
        }

        return list;
    }
}
